package com.moli.iov.down.command.design.command.reshandler.impl;

import com.moli.iov.down.model.dto.req.VehicleParamDto;
import com.moli.iov.down.command.codec.decode.bean.QueryResBean;
import com.moli.iov.down.command.codec.decode.bean.extend.QueryResExtendBean;
import com.moli.iov.down.command.codec.decode.decoders.QueryDecoder;
import com.moli.iov.down.command.codec.utils.Protocol;
import com.moli.iov.down.command.design.command.param.result.ParamQueryResultCommand;
import com.moli.iov.down.command.design.command.reshandler.ResponseHandlerCommand;
import com.moli.iov.down.command.design.factory.param.ParamQueryResultFactory;
import com.moli.iov.down.command.mq.SendCache;
import com.moli.iov.down.service.IVehicleParamService;
import com.moli.iov.util.SpringContextUtils;
import java.util.Date;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * <p>
 * 终端参数查询结果上报应答
 * </p>
 *
 * @author: zql
 * @Date: 2018/12/29 17:37
 */
public class TerminalParameterQueryCommand implements ResponseHandlerCommand {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    private SendCache sendCache = SpringContextUtils.getBean("sendCache");

    private IVehicleParamService vehicleParamService = SpringContextUtils.getBean("vehicleParamService");

    @Override
    public void execute(Protocol protocol) {
        String vin = protocol.getVin();
        byte command = protocol.getCommandTag();
        //终端参数查询结果上报应答 业务处理
        QueryDecoder queryDecoder = SpringContextUtils.getBean("queryDecoder");
        QueryResBean decode = queryDecoder.decode(protocol.getData());
        decode.setVin(vin);
        String serialNumber = String.valueOf(decode.getSerialNumber());

        parameterQueryHandle(decode);

        sendCache.updateResResult(vin, command, serialNumber);
    }

    /**
     *
     * 参数查询响应 handle
     *
     * @param decode
     */
    public void parameterQueryHandle(QueryResBean decode){
        logger.info("[上行响应报文] [参数查询响应] remoteControlResBean：{}", decode);
        //设置结果
        VehicleParamDto vehicleParamDto = setVehicleParamReqDto(decode);

        //更新数据库
        updateVehicleParam(vehicleParamDto);

        //更新缓存
        sendCache.setParameterQueryCache(decode);
    }

    /**
     * 设置 响应结果
     * @param decode
     */
    public VehicleParamDto setVehicleParamReqDto(QueryResBean decode){

        VehicleParamDto vehicleParamDto = new VehicleParamDto();
        for (QueryResExtendBean qreb : decode.getQueryResExtendBeans()) {
            try{
                ParamQueryResultCommand paramQueryCommand =
                    ParamQueryResultFactory.getParamQueryCommand(qreb.getParamId());

                vehicleParamDto = paramQueryCommand.execute(vehicleParamDto, qreb);
            }catch(Exception e){
                logger.error(String.format("设置参数查询响应结果 异常  参数：%s", vehicleParamDto), e);
            }
        }

        return vehicleParamDto;
    }

    /**
     * 更新参数查询响应结果
     */
    public void updateVehicleParam(VehicleParamDto vehicleParamDto){
        vehicleParamDto.setUpdateDate(new Date());
        vehicleParamService.updateVehicleParam(vehicleParamDto);
    }


}
